<?php
/**
 * 首页整合
 * 包括首页信息，分类信息，最组合，限时特购，画种导购
 */
define('IN_ECS', true);
require(dirname(__FILE__) . '/includes/init.php');

										/****************首页*************/
/* 拼接基地址 */
$baseUrl = $_SERVER['HTTP_HOST'] . '/app/indexPic/';
                                    
//轮显
$bannerPic = array(
	array('flag'=>1, 'title'=>'商品名称', 'pic'=>$baseUrl . 'index_banner_1.jpg', 'id'=>304 ,'link'=>''),
    array('flag'=>2, 'title'=>'活动名称', 'pic'=>$baseUrl . 'index_banner_2.jpg', 'id'=>''  ,'link'=>$_SERVER['HTTP_HOST']),
    array('flag'=>3, 'title'=>'宣传主题', 'pic'=>$baseUrl . 'index_banner_3.jpg', 'id'=>''  ,'link'=>$_SERVER['HTTP_HOST'])
    );
//功能列表
$listPic = array(
	array('flag'=>1,  'title'=>'最组合',   'pic'=>$baseUrl . 'index_banner_1.jpg'),
	array('flag'=>2,  'title'=>'限时特购', 'pic'=>$baseUrl . 'index_banner_2.jpg'),
	array('flag'=>3,  'title'=>'画种导购', 'pic'=>$baseUrl . 'index_banner_3.jpg')
	);

$bannerPic = tourlencode($bannerPic);
$listPic = tourlencode($listPic);

                                        /****************获得搜索推荐词*********/  
$keywords = array('毕莎罗颜料','调色盘','画板');                   //铅笔、扇形笔、马克笔、水粉笔
$keywords = tourlencode($keywords);                                                           
    

										/****************分类信息*************/
$cate = get_child_tree(145);
//var_dump($cate);
$i = 0;
while($i < count($cate))
{
	$cate[$i]['cateTree'] = get_child_tree($cate[$i]['id']);
    $i++;
}

/* 获得分类子树 */
function get_child_tree($tree_id = 145)
{
    $cate = array();
    $arr =  array();
    $sql = 'SELECT count(*) FROM ' . $GLOBALS['ecs']->table('category') . " WHERE parent_id = '$tree_id' AND is_show = 1 ";
    if ($GLOBALS['db']->getOne($sql) || $tree_id == 0)
    {
        $child_sql = 'SELECT cat_id, cat_name, parent_id, is_show ' .
                'FROM ' . $GLOBALS['ecs']->table('category') .
                "WHERE parent_id = '$tree_id' AND is_show = 1 ORDER BY sort_order ASC, cat_id ASC";
        $res = $GLOBALS['db']->getAll($child_sql);
        foreach ($res AS $row)
        {
            if ($row['is_show'])
			{
                $cate[$row['cat_id']]['id']   = $row['cat_id'];
                $cate[$row['cat_id']]['name'] = urlencode($row['cat_name']);
                $arr[] = $cate[$row['cat_id']];
            }
            
        }
        
    }
    return $arr;
}

  										/****************最组合*************/
$compose = get_compose();

/**
 * 获得最组合的数据
 * @return array{id,name,picture,marketPrice}
 */
function get_compose(){
    $sql = 'SELECT goods_id, goods_name, shop_price, goods_thumb FROM ' . $GLOBALS['ecs']->table('goods') . ' WHERE is_on_sale=1 AND cat_id=101';
    $res = $GLOBALS['db']->getAll($sql);

    $compose= array();

    foreach ($res AS $row)
    {
        if (!empty($row['goods_id']))
        {
            $compose[] = array( 'id' => $row['goods_id'],
            					'name' => urlencode($row['goods_name']),
                                'picture'  => $_SERVER['HTTP_HOST'] . '/' . $row['goods_thumb'],    //拼接成url
                                'marketPrice' => $row['shop_price']);
        }
    }
    return $compose;
}

										/****************限时特购*************/
$goodslist = get_promote_goods('',100);

										/****************画种导购*************/
/* 首部轮转图 */
$guideBanner = array(array('url' => 'url1'),
	                 array('url' => 'url2'), 
	                 array('url' => 'url3') 
                     );

/* 种类详情请求 */
$allow_cat_id = array(197, 199, 200, 201, 203, 204, 205);

/* 获得请求的分类 ID */
if (isset($_REQUEST['cat_id']) && in_array($_REQUEST['cat_id'], $allow_cat_id)){
    $cat_id = intval($_REQUEST['cat_id']);
}
else{
    $cat_id = 197;
}

if($cat_id==197){
    $goods_list = category_get_goods(get_children($cat_id),  "10000", "1", "sort_order","ASC");
}else{
    $goods_list = category_get_goods(get_children($cat_id),  "10000", "1", "goods_id","ASC");
}

/**
 * 获得分类下的商品
 *
 * @access  public
 * @param   string  $children
 * @return  array
 */
function category_get_goods($children, $pagesize, $page, $sort, $order)
{
    
    $where = "g.is_on_sale = 1 AND g.is_alone_sale = 1 AND ".
            "g.is_delete = 0 AND ($children OR " . get_extension_goods($children) . ')';

    /* 获得商品列表 */
    $sql = 'SELECT g.goods_id, g.goods_name, g.click_count, g.shop_price, g.market_price, g.promote_price, g.promote_start_date, g.promote_end_date, g.goods_thumb , g.goods_img, g.goods_type,' .
                "(select sum(goods_number) from " . $GLOBALS['ecs']->table('order_goods') . " WHERE goods_id = g.goods_id ) AS buy_sum ".
                'FROM ' . $GLOBALS['ecs']->table('goods') . ' AS g ' . 
                "WHERE $where ORDER BY $sort $order";

    $res = $GLOBALS['db']->selectLimit($sql, $pagesize, ($page - 1) * $pagesize);

    $arr = array();
    while ($row = $GLOBALS['db']->fetchRow($res))
    {
        if ($row['promote_price'] > 0)
        {
            $promote_price = bargain_price($row['promote_price'], $row['promote_start_date'], $row['promote_end_date']);
        }
        else
        {
            $promote_price = 0;
        }

        $arr[$row['goods_id']]['id']         = $row['goods_id'];
        $arr[$row['goods_id']]['name']       = urlencode($row['goods_name']);
        $arr[$row['goods_id']]['clickCount']      = $row['click_count'];
        $arr[$row['goods_id']]['marketPrice']     = $row['market_price']!=0 ? $row['market_price'] : (string)($row["shop_price"]*1.2);
        $arr[$row['goods_id']]['shopPrice']       = $row['shop_price'];
        //$arr[$row['goods_id']]['type']             = $row['goods_type'];
        $arr[$row['goods_id']]['promotePrice']    = (string)$promote_price;
        $arr[$row['goods_id']]['picture']      = urldecode($_SERVER['HTTP_HOST'] . '/' . $row['goods_thumb']);   //小图
        //$arr[$row['goods_id']]['goods_img']        = $row['goods_img'];    //中图
        //$arr[$row['goods_id']]['url']              = build_uri('goods', array('gid'=>$row['goods_id']), $row['goods_name']);
        $arr[$row['goods_id']]['buySum']          = $row['buy_sum'];
        //$arr[$row['goods_id']]['thumb_list']       = get_thumb($row['goods_id']);
        
        $return[] = $arr[$row['goods_id']];          //去掉数组索引
    }
    

    return $return;
}

                                    /****************输出*************/
$response = array('response'=>'intergration', 'bannerPic'=>$bannerPic, 'keywords' => $keywords, 'listPic'=>$listPic,'categoryInfo' => $cate, 'compose' => $compose, 'limitGoods'=>$goodslist, 'limitCount'=>count($goodslist), 'guideBanner'=>$guideBanner, 'goodslist'=>$goods_list);
echo tourldecode($response);
?>